# facet
library(ggplot2)

mpg2 <- subset(mpg, cyl != 5 & drv %in% c("4", "f"))

# facet grid----
qplot(cty, hwy, data = mpg2)
qplot(cty, hwy, data = mpg2) + facet_null()

# One row, multi-column (. ~ var)
qplot(cty, hwy, data = mpg2) + facet_grid(. ~ cyl)

# One col, multi-row (var ~ .)
qplot(cty, data = mpg2, geom = "histogram", binwidth = 2) + 
  facet_grid(cyl ~ .)

# Multi-row and multi-column
qplot(cty, hwy, data = mpg2) +
  facet_grid(drv ~ cyl)

# . ~ a + b / a + b ~ .

# margin plot----
p <- qplot(displ, hwy, data = mpg2) +
  geom_smooth(method = "lm", se = FALSE)
p + facet_grid(cyl ~ drv)
p + facet_grid(cyl ~ drv, margins = TRUE)

p <- qplot(displ, hwy, data = mpg2) +
  geom_smooth(aes(colour = drv), method = "lm", se = FALSE) +
  facet_grid(cyl ~ drv, margins = TRUE)
p

# scale control----
p <- qplot(cty, hwy, data = mpg)
p + facet_wrap(~ cyl)
p + facet_wrap(~ cyl, scales = "free")  # Scale control

library(reshape2)
em <- melt(economics, id = "date")
qplot(date, value, data = em, geom = "line", group = variable) +
  facet_grid(variable ~ ., scales = "free_y")


mpg3 <- within(mpg2, {
  model <- reorder(model, cty)
  manfacturer <- reorder(manufacturer, -cty)
})

models <- qplot(cty, model, data = mpg3)
models
models + facet_grid(manufacturer ~ ., scales = "free", space = "free") +
  theme(strip.text.y = element_text())

# Group and facet----
xmaj <- c(0.3, 0.5, 1.3, 5)
xmin <- as.vector(outer(1:10, 10^c(-1, 0)))
ymaj <- c(500, 1000, 5000, 10000)
ymin <- as.vector(outer(1:10, 10^c(2,3,4)))
dplot <- ggplot(subset(diamonds, color %in% c("D", "E", "G", "J")),
                aes(carat, price, colour = color)) +
  scale_x_log10(breaks = xmaj, labels = xmaj, minor = xmin) +
  scale_y_log10(breaks = ymaj, labels = ymaj, minor = ymin) +
  scale_colour_hue(limits = levels(diamonds$color)) +
  theme(legend.position = "none")

dplot + geom_point()
dplot + geom_point() + facet_grid(. ~ color)
dplot + geom_smooth(method = lm, se = FALSE, fullrange = TRUE)
dplot + geom_smooth(method = lm, se = FALSE, fullrange = TRUE) +
  facet_grid(. ~ color)

# Parallel arrange----
qplot(color, data = diamonds, geom = "bar", fill = cut, position = "dodge")
qplot(cut, data = diamonds, geom = "bar", fill = cut) +
  facet_grid(. ~ color) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, 
                                   size = 8, colour = "grey50"))

mpg4 <- subset(mpg, manufacturer %in% c('audi', 'volkswagen', 'jeep'))
mpg4$manufacturer <- as.character(mpg4$manufacturer)
mpg4$model <- as.character(mpg4$model)

base <- ggplot(mpg4, aes(fill = model)) +
  geom_bar(position = "dodge") +
  theme(legend.position = "none")

base + aes(x = model) +
  facet_grid(. ~ manufacturer)
last_plot() + 
  facet_grid(. ~ manufacturer, scales = "free_x", space = "free")
base + aes(x = manufacturer)

# facet for continous variable----
mpg2$disp_ww <- cut_interval(mpg2$displ, length = 1)
mpg2$disp_wn <- cut_interval(mpg2$displ, n = 6)
mpg2$disp_nn <- cut_number(mpg2$displ, n = 6)

plot <- qplot(cty, hwy, data = mpg2) + labs(x = NULL, y = NULL)
plot + facet_wrap(~ disp_ww, nrow = 1)
plot + facet_wrap(~ disp_wn, nrow = 1)
plot + facet_wrap(~ disp_nn, nrow = 1)
